<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="模板名称" prop="templateName">
        <el-input v-model="queryParams.templateName" placeholder="请输入模板名称" clearable />
      </el-form-item>
      <el-form-item class="mbname leftsearch">
        <el-button
          @click="getList()"
          type="primary"
          size="small"
        >&nbsp; &nbsp; &nbsp;查询 &nbsp; &nbsp; &nbsp;</el-button>
        <el-button
          @click="resetFormMessage()"
          type="info"
          size="small"
        >&nbsp; &nbsp; &nbsp;重置 &nbsp; &nbsp; &nbsp;</el-button>
      </el-form-item>
    </el-form>
    <div class="right_button">
      <el-button
        type="primary"
        size="small"
        @click.stop="handleUpdate('','add')"
      >&nbsp; &nbsp; &nbsp; 新增 &nbsp; &nbsp; &nbsp;</el-button>
    </div>
    <el-table v-loading="loading" :data="messageList">
      <el-table-column label="模板编号" align="center" prop="templateId" width="150" />
      <el-table-column
        label="消息模板名称"
        align="center"
        prop="templateName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="模板内容" align="center" prop="messageContent" min-width="150" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="info"
            style="width:60px;"
            @click="handleUpdate(scope.row,'read')"
          >查看</el-button>
          <el-button
            size="mini"
            type="success"
            style="width:60px;"
            @click="handleUpdate(scope.row,'update')"
          >修改</el-button>
          <el-button
            size="mini"
            type="danger"
            style="width:60px;"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改消息模板弹出框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="dialogRef" :model="dialogForm" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="消息名称" prop="templateName">
              <el-input
                :disabled="disabled"
                v-model="dialogForm.templateName"
                placeholder="请输入消息名称"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="标识码" prop="identificationCode">
              <el-input
                :disabled="disabled"
                v-model="dialogForm.identificationCode"
                placeholder="请输入标识码"
              />
              <p style="margin:0px;">标识码用于识别接口调用，具有唯一性</p>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="接收方式" prop="receiveType">
              <el-checkbox-group :disabled="disabled" v-model="receiveType">
                <el-checkbox label="dx">短信</el-checkbox>
                <el-checkbox label="yj">邮件</el-checkbox>
                <el-checkbox label="znx">站内信</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="消息标题" prop="messageTitle">
              <el-input
                :disabled="disabled"
                v-model="dialogForm.messageTitle"
                placeholder="请输入消息标题"
              />
              <p style="margin:0px;">名称由数字、中文、英文、下划线、+、-组成。</p>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="消息内容" prop="messageContent">
              <el-input
                :disabled="disabled"
                type="textarea"
                :rows="4"
                placeholder="请输入消息内容"
                v-model="dialogForm.messageContent"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm()">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import { insertOrUpdateMessageConfig } from "@/api/system/message";
import axios from "axios";
export default {
  name: "Notice",
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        templateName: "",
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      dialogForm: {
        templateName: "",
        identificationCode: "",
        messageTitle: "",
        messageContent: "",
        receiveType: [],
      },
      disabled: false,
      receiveType: [],
      rowArry: {},
      type: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公告列表 */
    async getList() {
      this.loading = true;
      try {
        const response = await axios.post(
          process.env.VUE_APP_BASE_API +
            "/message/queryMessageConfigList?pageNum=" +
            this.queryParams.pageNum +
            "&pageSize=" +
            this.queryParams.pageSize,
          { templateName: this.queryParams.templateName }
        );
        this.messageList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      } catch (error) {
        this.loading = false;
        console.error("Error fetching users:", error);
      }
    },
    checkFormMessage() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.$modal.msgSuccess("查询成功");
    },
    // 表单重置
    resetFormMessage() {
      this.queryParams.templateName = "";
    },
    /** 新增按钮操作 */
    handleUpdate(row, type) {
      if (type === "add") {
        this.type = type;
        this.open = true;
        this.title = "添加文章";
        this.dialogForm = {
          templateName: "",
          messageName: "",
          identificationCode: "",
          messageTitle: "",
          messageContent: "",
          receiveType: [],
        };
        this.receiveType = [];
      } else if (type === "update") {
        this.type = type;
        this.rowArry = row;
        this.open = true;
        this.disabled = false;
        this.title = "修改文章";
        this.dialogForm.templateName = row.templateName;
        this.dialogForm.identificationCode = row.identificationCode;
        this.dialogForm.messageTitle = row.messageTitle;
        this.dialogForm.messageContent = row.messageContent;
        this.receiveType = row.receiveType.split(",");
      } else {
        this.type = "read";
        console.log(row);
        this.disabled = true;
        this.open = true;
        this.title = "查看文章";
        this.dialogForm.templateName = row.templateName;
        this.dialogForm.identificationCode = row.identificationCode;
        this.dialogForm.messageTitle = row.messageTitle;
        this.dialogForm.messageContent = row.messageContent;
        this.receiveType = row.receiveType.split(",");
      }
    },
    handleDelete(row) {
      this.$modal
        .confirm("确定要删除吗？")
        .then(async () => {
          try {
            await axios.post(
              process.env.VUE_APP_BASE_API + "/message/deleteMessageConfigById",
              { templateId: row.templateId }
            );
            this.$modal.msgSuccess("删除成功");
            this.getList();
          } catch (error) {
            this.loading = false;
            console.error("Error fetching users:", error);
          }
        })
        .catch(function () {});
    },
    submitForm() {
      let { templateName, identificationCode, messageTitle, messageContent } =
        this.dialogForm;
      if (
        templateName === "" ||
        identificationCode === "" ||
        messageTitle === "" ||
        messageContent === "" ||
        this.receiveType == [] ||
        this.receiveType.length === 0
      ) {
        return this.$modal.msgError("添加文章所有内容为必填，请填写完整");
      }
      console.log(this.type, "type====");
      if (this.type == "read") {
        this.open = false;
      } else if (this.type == "update") {
        this.dialogForm.receiveType = this.receiveType.toString();
        insertOrUpdateMessageConfig({
          ...this.dialogForm,
          templateId: this.rowArry.templateId,
        })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("修改成功");
          })
          .catch(() => {
            this.getList();
            this.$modal.msgError("修改失败");
          });
        this.open = false;
      } else if (this.type == "add") {
        this.dialogForm.receiveType = this.receiveType.toString();
        insertOrUpdateMessageConfig(this.dialogForm)
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("新增成功");
          })
          .catch(() => {
            this.getList();
            this.$modal.msgError("新增失败");
          });
        this.open = false;
      }
    },
  },
};
</script>
  